package stark.app.function.news;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import org.jetbrains.annotations.NotNull;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import stark.app.R;
import stark.app.databinding.ActivityNewsDemoBinding;
import stark.common.apis.constant.NewsType;
import stark.common.basic.base.BaseNoModelActivity;

/**
 * Author：daizhongbin on 2022/12/1
 * Email：1027154338@qq.com
 * Desc:
 */
public class NewsDemoActivity extends BaseNoModelActivity<ActivityNewsDemoBinding> {
    @Override
    protected int onCreate() {
        return R.layout.activity_news_demo;
    }

    @Override
    protected void initView() {
        //mDataBinding.viewPager.setOffscreenPageLimit(NewsType.values().length);
        mDataBinding.viewPager.setAdapter(new MyPagerAdapter(this));
        TabLayoutMediator mediator = new TabLayoutMediator(mDataBinding.tbLayout, mDataBinding.viewPager, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull @NotNull TabLayout.Tab tab, int position) {
                tab.setText(NewsType.values()[position].getName());
            }
        });
        mediator.attach();
    }

    @Override
    protected void initData() {
    }

    private class MyPagerAdapter extends FragmentStateAdapter {

        public MyPagerAdapter(@NonNull @NotNull FragmentActivity fragmentActivity) {
            super(fragmentActivity);
        }

        @NonNull
        @NotNull
        @Override
        public Fragment createFragment(int position) {
            return new NewsListFragment(NewsType.values()[position]);
        }

        @Override
        public int getItemCount() {
            return NewsType.values().length;
        }

    }
}
